# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Pablo Correa Gómez <pabloyoyoista@postmarketos.org>
pkgname=glib
pkgver=2.82.2
_gi_version=1.82.0
pkgrel=0
pkgdesc="Common C routines used by Gtk+ and other libs"
url="https://developer.gnome.org/glib/"
arch="all"
license="LGPL-2.1-or-later"
triggers="$pkgname.trigger=/usr/share/glib-2.0/schemas:/usr/lib/gio/modules:/usr/lib/gtk-4.0"
depends_dev="
	bzip2-dev
	docbook-xml
	docbook-xsl
	gettext-dev
	libxml2-utils
	libxslt
	python3
	py3-packaging
	"
makedepends="$depends_dev
	bash
	bison
	flex
	libffi-dev
	meson
	pcre2-dev
	py3-setuptools
	python3-dev
	util-linux-dev
	zlib-dev
	py3-docutils
	"
subpackages="
	$pkgname-dbg
	$pkgname-doc
	$pkgname-static
	$pkgname-dev
	$pkgname-lang
	$pkgname-bash-completion
	"
source="https://download.gnome.org/sources/glib/${pkgver%.*}/glib-$pkgver.tar.xz
	https://download.gnome.org/sources/gobject-introspection/${_gi_version%.*}/gobject-introspection-$_gi_version.tar.xz
	tests-machine-id.patch
	flaky-tests.patch
	"

# 2.80 moves gir here
replaces="gobject-introspection<1.80.0"

[ "$CARCH" = "ppc64le" ] && options="$options !check" # https://gitlab.gnome.org/GNOME/glib/-/issues/3474

# secfixes:
#   2.80.1-r0:
#     - CVE-2024-34397
#   2.66.6-r0:
#     - CVE-2021-27219 GHSL-2021-045
#   2.62.5-r0:
#     - CVE-2020-6750
#   2.60.4-r0:
#     - CVE-2019-12450

build() {
	export CFLAGS="$CFLAGS -ffat-lto-objects -O2"
	export CXXFLAGS="$CXXFLAGS -O2"
	export CPPFLAGS="$CPPFLAGS -O2"
	local _prefix="$PWD/boostrap-glib"

	msg "build bootstrap glib"
	meson setup \
		--default-library=shared \
		--prefix="$_prefix" \
		-Dman-pages=disabled \
		-Dlibmount=disabled \
		-Dtests=false \
		-Dintrospection=disabled \
		-Dnls=disabled \
		. output
	meson compile -C output
	meson install --no-rebuild -C output

	msg "build bootstrap gobject-introspection"
	meson setup \
		--pkg-config-path="$_prefix"/lib/pkgconfig \
		--prefix="$_prefix" \
		-Dbuild_introspection_data=false \
		-Dcairo=disabled \
		-Ddoctool=disabled \
		"$srcdir"/gobject-introspection-$_gi_version \
		gioutput
	meson compile -C gioutput
	meson install --no-rebuild -C gioutput

	msg "re-build glib with introspection"
	PATH="$_prefix/bin:$PATH" LD_LIBRARY_PATH="$_prefix/lib" \
		meson setup \
		--reconfigure \
		--pkg-config-path="$_prefix"/lib/pkgconfig \
		--default-library=both \
		--prefix=/usr \
		-Dman-pages=enabled \
		-Dlibmount=enabled \
		-Dtests="$(want_check && echo true || echo false)" \
		-Dintrospection=enabled \
		-Dnls=enabled \
		. output
	PATH="$_prefix/bin:$PATH" LD_LIBRARY_PATH="$_prefix/lib" \
		meson compile -C output
}

check() {
	# riscv64 and armhf may time out with the standard timeout
	meson test --timeout-multiplier 10 --no-rebuild --print-errorlogs -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output
}

dev() {
	# 2.80 moves gir here
	replaces="gobject-introspection-dev<1.80.0"
	default_dev
	mkdir -p "$subpkgdir"/usr/bin "$subpkgdir"/usr/share
	find "$pkgdir"/usr/bin ! -name "glib-compile-schemas" -a \( \
		-name "gdbus-codegen" -o \
		-name "gobject-query" -o \
		-name "gresource" -o \
		-name "gtester*" -o \
		-name "glib-*" \) \
		-exec mv {} "$subpkgdir"/usr/bin \;

	amove usr/share/gdb usr/share/glib-2.0
}

static() {
	default_static
	depends="gettext-static"
}

sha512sums="
d5b761ec0c088496f2f9e5767568076a30ff492c272f21133560689f4e6a177d403c78d71ef85da63de331152c4d1f27b65d3e927ebfaf0fb63592f66e564ea1  glib-2.82.2.tar.xz
e139fadb4174c72b648914f3774d89fc0e5eaee45bba0c13edf05de883664dad8276dbc34006217bb09871ed4bad23adab51ff232a17b9eb131329b2926cafb7  gobject-introspection-1.82.0.tar.xz
4f561bfede4a559b314195aa4b7b5923a52c62063e6dd33b4933ad53b29c32ef789d69b9f7b17d8d701323006366e6b7acbb083a68674c1c535c185d35cc5d1f  tests-machine-id.patch
268cf6f089274f29285242f1e29bf5650e85f9fcb5744df884deb139493cc25792ad78487113732665f1c5393c94b2f76f97093a1a003e3caaf65c07960a1bf9  flaky-tests.patch
"
